package com.hospital.wx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.hospital.wx.controller.form.UpdatePatientUserInfoForm;
import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.InsertPatientUserInfoForm;
import com.hospital.wx.pojo.PatientUserInfo;
import com.hospital.wx.service.PatientUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping("/patient/user/info")
@Tag(name = "PatientUserInfoController", description = "患者详细信息记录接口")
@Slf4j
public class PatientUserInfoController {
    @Autowired
    private PatientUserInfoService patientUserInfoService;
    @PostMapping("/insert")
    @SaCheckLogin
    @Operation(summary = "就诊卡信息保存")
    public CommonResult insert(@RequestBody @Valid InsertPatientUserInfoForm form){
        try {
            PatientUserInfo patientUserInfo = BeanUtil.toBean(form, PatientUserInfo.class);
            int userid = StpUtil.getLoginIdAsInt();
            String uuid = IdUtil.simpleUUID();
            patientUserInfo.setUserId(userid);
            patientUserInfo.setUuid(uuid);
            patientUserInfo.setExistFaceModel(false);
            String medicalHistory = JSONUtil.parseArray(form.getMedicalHistory()).toString();
            patientUserInfo.setMedicalHistory(medicalHistory);
            patientUserInfoService.insert(patientUserInfo);
            return CommonResult.ok();
        } catch (Exception e) {
            // 记录异常日志
            log.error("就诊卡信息保存失败", e);
            return CommonResult.error(2002,"就诊卡信息保存失败");
        }
    }
    @GetMapping("/selectPatientUserInfoByUserId")
    @SaCheckLogin
    @Operation(summary = "诊卡信息查询接口开发")
    public CommonResult selectPatientUserInfoByUserId() {
        try {
            // 基于 token 获得 userId
            int userId = StpUtil.getLoginIdAsInt();

            // 查询患者信息
            HashMap<String, Object> map = patientUserInfoService.selectPatientUserInfoByUserId(userId);

            // 判断查询结果是否为空
            if (MapUtil.isEmpty(map)) {
                return CommonResult.ok(CommonResult.error(2003,"就诊卡信息查询失败"));
            }
            return CommonResult.ok(map);
        } catch (Exception e) {
            // 记录异常日志
            log.error("查询患者信息时出错，userId: {}", StpUtil.getLoginIdAsInt(), e);

            // 返回通用的失败信息
            return CommonResult.error(2003,"就诊卡信息查询失败");
        }
    }

    @PostMapping("/update")
    @SaCheckLogin
    @Operation(summary = "就诊卡信息更新")
    public CommonResult update(@RequestBody @Valid UpdatePatientUserInfoForm form) {
        try {
            // 将表单数据转换为 PatientUserInfo 对象
            PatientUserInfo patientUserInfo = BeanUtil.toBean(form, PatientUserInfo.class);
            // 将 MedicalHistory 由数组转换为字符串
            String json = JSONUtil.parseArray(form.getMedicalHistory()).toString();
            patientUserInfo.setMedicalHistory(json);
            // 执行更新操作
            patientUserInfoService.update(patientUserInfo);
            // 返回成功结果
            return CommonResult.ok();
        } catch (Exception e) {
            // 记录异常日志
            log.error("更新患者信息时出错，userId: {}", StpUtil.getLoginIdAsInt(), e);
            // 返回统一的失败信息
            return CommonResult.error(2003,"就诊卡信息查询失败");
        }
    }
}
